package global;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;

import com.google.common.collect.Maps;

import server.Initialize;
import server.db.StmtHelper;
import server.objects.User;
import fj.F;
import fj.F2;
import fj.F3;

public class Global {

	
	HttpServletRequest request;
	
	public Global(HttpServletRequest request)
	{
		this.request=request;
	}
	
	
	public F<HttpServletRequest,Boolean> isLoggedIn = new F<HttpServletRequest,Boolean>()
	{

		@Override
		public Boolean f(HttpServletRequest req) {
			return sessionAttrHasValue.f(req, "user");
		}
		
	};
	
	public static F2<HttpServletRequest,String,Boolean> sessionAttrHasValue =  new F2<HttpServletRequest,String,Boolean>() 
	{

		@Override
		public Boolean f(HttpServletRequest req, String attrName) {
			return req.getSession()!=null && req.getSession().getAttribute(attrName)!=null;
		}
		
	};


	public Boolean login(String email, String passwd,Boolean checkCredentials) {
		
		Boolean valid=true;
		if(checkCredentials)
		{
			
			try {
				Map<String,String> userFields = StmtHelper.db("select u.iduser,u.email,u.firstname,u.lastname,(select " +
						"ut.code from usertype ut where ut.idusertype=u.idusertype) usertype from user u where u.email=").addVariable(email)
							.addSql(" and u.password=").addVariable(passwd).collectIntoMaps.unsafeGet().get(0);
				if(userFields.size()<1)
					return false;
				User u = new User();
				u.firstname=userFields.get("firstname");
				u.lastname=userFields.get("lastname");
				u.email=userFields.get("email");
				u.usertype=userFields.get("usertype");
				u.id=Integer.parseInt(userFields.get("iduser"));
				setUser(u);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			
		return valid;
	}


	public void setUser(User user) {
		request.getSession().setAttribute("user", user);
	}


	public User getUser() {
		User u = (User)request.getSession().getAttribute("user");
		if(u==null && request.getCookies()!=null)
		{
			/*
			Cookie[] cookies = request.getCookies();

			for(Cookie cookie : cookies)
			{
				if(cookie.getName().equals("user"))
					try {
						loginNoPassword(URLDecoder.decode(cookie.getValue(),"utf-8"));
						return getUser();
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
			}
			*/
		}
		return u;
	}


	private void loginNoPassword(String email) {
		try {
			Map<String,String> userFields = StmtHelper.db("select email,firstname,lastname,iduser from user where email=").addVariable(email)
						.collectIntoMaps.unsafeGet().get(0);
			User u = new User();
			u.firstname=userFields.get("firstname");
			u.lastname=userFields.get("lastname");
			u.email=userFields.get("email");
			u.id=Integer.parseInt(userFields.get("id"));
			setUser(u);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public void logout() {
		setUser(null);
	}


	public void setListingResults(JSONArray jsonArr) {
		request.getSession().setAttribute("listingResults", jsonArr);
	}
	public JSONArray getListingResults()
	{
		return (JSONArray)request.getSession().getAttribute("listingResults");
	}
}
